package db

import "gorm.io/gorm"

type DbInterface interface {
	connect() *gorm.DB
	setUserName(name string)
	setPassword(pass string)
	setName(name string)
	setPort(port string)
	setHost(host string)
	setPrefix(prefix string)
}

type Db struct {
	db      DbInterface
	connect *gorm.DB
}

func Create(driver string) *Db {
	db := &Db{}
	db.createConnect(driver)
	return db
}

func (d *Db) createConnect(driver string) {
	switch driver {
	case "mysql":
		d.db = mysqlCreate()
	default:
		panic("驱动不存在")
	}
}

func (d *Db) UserName(name string) *Db {
	d.db.setUserName(name)
	return d
}

func (d *Db) Password(pwd string) *Db {
	d.db.setPassword(pwd)
	return d
}

func (d *Db) Name(name string) *Db {
	d.db.setName(name)
	return d
}

func (d *Db) Port(prot string) *Db {
	d.db.setPort(prot)
	return d
}

func (d *Db) Host(host string) *Db {
	d.db.setHost(host)
	return d
}

func (d *Db) Prefix(prefix string) *Db {
	d.db.setPrefix(prefix)
	return d
}

func (d *Db) Run() {
	d.connect = d.db.connect()
}

func (d *Db) GetConnect() *gorm.DB {
	return d.connect
}
